package com.htfyun.factorytest.factorytestui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.htfyun.factorytest.view.CrossView;

import com.htfyun.factorytest.R;
import com.htfyun.factorytest.utils.DisplayUtil;

public class FactoryPenAccuracyActivity extends FactoryBaseActivity {
    private RelativeLayout parentContainer;
    private CrossView crossViews1, crossViews2, crossViews3, crossViews4, crossViews5;
    private static final int margin = 150;
/*    private ServiceConnection serviceConnection = new MyServiceConnection();
    private FTFloatWindowService mBindService;

    private class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {

            FTFloatWindowService.MyBinder mBinder = (FTFloatWindowService.MyBinder) binder;
            mBindService = mBinder.getService();

            if (mBindService != null) {
                mBindService.setFloatLayoutVisibility(View.INVISIBLE);
            }

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBindService = null;
        }

    }*/

    @Override
    int getLayoutId() {
        return R.layout.activity_factory_pen_accuracy;
    }

    @Override
    void onInitView(@Nullable Bundle savedInstanceState) {
/*        Intent intent = new Intent(mContext, FTFloatWindowService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);*/

        int width = DisplayUtil.getScreenWidth(mContext);
        int height = DisplayUtil.getScreenHeight(mContext);

        crossViews1 = new CrossView(mContext, margin, margin);
        crossViews2 = new CrossView(mContext, width - margin, margin);
        crossViews3 = new CrossView(mContext, margin, height - margin);
        crossViews4 = new CrossView(mContext, width - margin, height - margin);
        crossViews5 = new CrossView(mContext, width / 2, height / 2);
        parentContainer = findViewById(R.id.root_layout);
        parentContainer.addView(crossViews1);
        parentContainer.addView(crossViews2);
        parentContainer.addView(crossViews3);
        parentContainer.addView(crossViews4);
        parentContainer.addView(crossViews5);
    }

    @Override
    void onHandleMessage(Handler handler, Message msg) {

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
//        DebugLog.e("event = " + x + " " + y);
        if (event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS) {
            testFinished(crossViews1, x, y);
            testFinished(crossViews2, x, y);
            testFinished(crossViews3, x, y);
            testFinished(crossViews4, x, y);
            testFinished(crossViews5, x, y);
        }
        return super.onTouchEvent(event);
    }

    private void testFinished(CrossView crossView, int x, int y) {
        if (crossView.testInRange(x, y)) {
            parentContainer.removeView(crossView);
            if (parentContainer.getChildCount() == 0) {
                finishWithOK();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
/*        if (mBindService != null) {
            mBindService.setFloatLayoutVisibility(View.INVISIBLE);
        }*/
    }

    @Override
    protected void onPause() {
        super.onPause();
/*        if (mBindService != null) {
            mBindService.setFloatLayoutVisibility(View.VISIBLE);
        }*/
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        unbindService(serviceConnection);
    }

}
